package com.mall.shop.utils;

import com.mall.shop.enums.HttpCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一返回对象R
 *
 * @author 龙茶清欢
 * @since 2022-11-06 12:30:55
 */
@Getter
@Setter
@Accessors(chain = true)
public class R<T> implements Serializable {
	private static final long serialVersionUID= -68551074690715808L;

    private int code;
	private String msg;
	private T data;

    public static <T> R<T> ok() {
		return restResult(null, HttpCode.OK.getCode(), HttpCode.OK.getMsg());
	}

	public static <T> R<T> ok(T data) {
		return restResult(data, HttpCode.OK.getCode(), HttpCode.OK.getMsg());
	}

	public static <T> R<T> ok(String msg) {
		return restResult(null, HttpCode.OK.getCode(), msg);
	}

	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, HttpCode.OK.getCode(), msg);
	}

	public static <T> R<T> failed() {
		return restResult(null, HttpCode.NOT_FOUND.getCode(), HttpCode.NOT_FOUND.getMsg());
	}

	public static <T> R<T> failed(String msg) {
		return restResult(null, HttpCode.NOT_FOUND.getCode(), msg);
	}

	public static <T> R<T> failed(T data) {
		return restResult(data, HttpCode.NOT_FOUND.getCode(), HttpCode.NOT_FOUND.getMsg());
	}

	public static <T> R<T> failed(T data, String msg) {
		return restResult(data, HttpCode.NOT_FOUND.getCode(), msg);
	}

	public static <T> R<T> failed(T data, int code, String msg) {
		return restResult(data, code, msg);
	}

	public static <T> R<T> restResult(T data, int code, String msg) {
		R<T> response = new R<>();
		response.setData(data);
		response.setCode(code);
		response.setMsg(msg);
		return response;
	}
	
}

// @Accessors(chain = true)  允许链式赋值

